//USAGE [ehs=caption]http://host.domain/image.jpg[/ehs]

global $pref, $e107;

$code_text   = $tp->toAttribute($code_text);
$parm   = $tp->toAttribute($parm);
list($caption_text, $params) = explode(":", $parm);
list($param_width, $param_hight) = explode(",", $params);
$param_width = ereg_replace("[^0-9]", "", $param_width);
$param_hight = ereg_replace("[^0-9]", "", $param_hight);

if ($pref['hs_image_on']) {

if(!function_exists('get_headers')) {

    function get_headers($url,$format=0) {
        $url_info=parse_url($url);
        $port = isset($url_info['port']) ? $url_info['port'] : 80;
        $fp=fsockopen($url_info['host'], $port, $errno, $errstr, 30);
        if($fp) {
            $head = "HEAD ".@$url_info['path']."?".@$url_info['query'];
            $head .= " HTTP/1.0\r\nHost: ".@$url_info['host']."\r\n\r\n";
            fputs($fp, $head);
            while(!feof($fp)) {
                if($header=trim(fgets($fp, 1024))) {
                    if($format == 1) {
                        $h2 = explode(':',$header);
                        // the first element is the http header type, such as HTTP/1.1 200 OK,
                        // it doesn't have a separate name, so we have to check for it.
                        if($h2[0] == $header) {
                            $headers['status'] = $header;
                        }
                        else {
                            $headers[strtolower($h2[0])] = trim($h2[1]);
                        }
                    }
                    else {
                        $headers[] = $header;
                    }
                }
            }
            return $headers;
        }
        else {
            return false;
        }
    }
}

$file_support = FALSE;
$ext_list = "jpg JPG jpeg JPEG gif GIF png PNG";
list($dirname, $basename, $extension, $filename) = array_values(pathinfo($code_text));
if ( substr_count($ext_list, $extension) > 0 ) $file_support = TRUE;
if (!$file_support) {
    $text = "<img src='".e_PLUGIN."eHighSlide/images/unsupported.png'>";
}

$file_exist = TRUE;
$file_headers = get_headers($code_text);
if ($file_headers[0] == 'HTTP/1.1 404 Not Found') {
  $text = "<img src='".e_PLUGIN."eHighSlide/images/badimg.png'>";
  $file_exist = FALSE;
}

if ($file_support and $file_exist) {

    $img_size = getimagesize($code_text);
    list($width_orig, $height_orig) = $img_size;
    $mime = $img_size['mime'];

    $cap_id = microtime(TRUE);

$image = "<img src='".e_PLUGIN."eHighSlide/foto.php?img=".$code_text."&h=".($param_hight ? $param_hight : $pref['hs_foto_icon_height'])."&w=".($param_width ? $param_width : $pref['hs_foto_icon_width'])."' alt='".($caption_text != "" ? $caption_text : "")."' title='".($caption_text != "" ? $caption_text : "")."'/>";
$link = "<a id='thumb_".$cap_id."' href='".e_PLUGIN."eHighSlide/foto.php?img=".$code_text."&h=".$pref['hs_foto_view_height']."&w=".$pref['hs_foto_view_width']."' class='highslide' onclick=\"return hs.expand(this, { captionId: 'caption_".$cap_id."' } )\">".$image."</a>";
$caption = "<div class='highslide-caption' id='caption_".$cap_id."'>".$tp->toHTML("".($caption_text != "" ? $caption_text."<br />" : "")."[link=".$code_text."]".($filename != "" ? $filename : $basename)."[/link]<br />".$mime." ".$width_orig."x".$height_orig."", true)."</div>";
//$text = "<a id='thumb_".$cap_id."' href='".e_PLUGIN."eHighSlide/foto.php?img=".$code_text."&h=".$pref['hs_foto_view_height']."&w=".$pref['hs_foto_view_width']."' class='highslide' onclick=\"return hs.expand(this, { captionId: 'caption_".$cap_id."' } )\"><img src='".e_PLUGIN."eHighSlide/foto.php?img=".$code_text."&h=".$pref['hs_foto_icon_height']."&w=".$pref['hs_foto_icon_width']."' alt='".($parm != "" ? $parm : "")."' title='".($parm != "" ? $parm : "")."'/></a><div class='highslide-caption' id='caption_".$cap_id."'>".$tp->toHTML("".($parm != "" ? $parm."<br />" : "")."[link=".$code_text."]".($filename != "" ? $filename : $basename)."[/link]<br />".$mime." ".$width_orig."x".$height_orig."", true)."</div>";
$text = "".$link."<noindex>".$caption."</noindex>";

}

return $text;

} else {

return $tp->toHTML("[link=".$code_text."]".$code_text."[/link]", true);

}